package managedbean.SaleInquiry;

import entity.LineItem;
import entity.Product;
import entity.SaleLead;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.PhaseEvent;
import session.stateless.SaleInquiryBeanLocal;
import session.stateless.SaleLeadBeanLocal;


@ManagedBean(name="saleInquiryManagerBean")
@ViewScoped
public class AddSaleInquiryManagerBean implements Serializable {
    public AddSaleInquiryManagerBean() {}
    
    @EJB
    private SaleInquiryBeanLocal saleInquiryBeanLocal;
    @EJB
    private SaleLeadBeanLocal saleLeadBeanLocal;
    
    private String saleLeadName;
    private List<Integer> quantity;
    private List<LineItem> lineItems;
    private Long saleInquiryId;
    private String statusMessage;
    
    public void initView(PhaseEvent event){
        try
        {            
            if(lineItems == null)
            {
                List<Product> products = saleInquiryBeanLocal.getProduct();
                lineItems = new ArrayList<LineItem>();

                for(Product product:products)
                {
                    LineItem lineItem = new LineItem();
                    lineItem.setProduct(product);

                    lineItems.add(lineItem);
                }
            }
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }
    
    public void initView2(PhaseEvent event){
        try
        {
            if(saleLeadName==null){
                SaleLead saleLead = (SaleLead)FacesContext.getCurrentInstance().getExternalContext().getFlash().get("selectedSaleLead");
                saleLeadName=saleLead.getSaleLeadName();
            }
            
            if(lineItems == null)
            {
                List<Product> products = saleInquiryBeanLocal.getProduct();
                lineItems = new ArrayList<LineItem>();

                for(Product product:products)
                {
                    LineItem lineItem = new LineItem();
                    lineItem.setProduct(product);

                    lineItems.add(lineItem);
                }
            }
        }        
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }
    
    public List<Integer> getQuantity() {
        return quantity;
    }
    public void setQuantity(List<Integer> quantity) {
        this.quantity = quantity;
    }

    public String getSaleLeadName() {
        return saleLeadName;
    }

    public void setSaleLeadName(String saleLeadName) {
        this.saleLeadName = saleLeadName;
    }
    
    public Long getSaleInquiryId() {
        return saleInquiryId;
    }

    public void setSaleInquiryId(Long saleInquiryId) {
        this.saleInquiryId = saleInquiryId;
    }

    public String getStatusMessage() {
        return statusMessage;
    }

    public void setStatusMessage(String statusMessage) {
        this.statusMessage = statusMessage;
    }

   
    public List<LineItem> getLineItems() {
        return lineItems;
    }
    public void setLineItems(List<LineItem> lineItems) {
        this.lineItems = lineItems;
    }
    
    public List<Product> getProduct(){
        return saleInquiryBeanLocal.getProduct();
    }
    
    public void saveSaleInquiry(ActionEvent event) {
        try {
            SaleLead saleLead = saleLeadBeanLocal.findByName(saleLeadName);
            if (saleLead==null) {
                statusMessage = "Sale lead does not exist.";
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Add New Sale Inquiry Result: Unsuccessful!  (" + statusMessage + ")", ""));
            } 
            else {
                List<LineItem> lineItemList = new ArrayList<LineItem>();
                LineItem temp = new LineItem();
                for (int i = 0; i < lineItems.size(); i++) {
                    if ((lineItems.get(i).getLineItemQuantity() != null)&&(lineItems.get(i).getLineItemQuantity()>0)) 
                    {
                        temp = saleInquiryBeanLocal.createLineItem(lineItems.get(i).getProduct(), lineItems.get(i).getLineItemQuantity());
                        lineItemList.add(temp);
                    }
                }
                saleInquiryId = saleInquiryBeanLocal.createSaleInquiry(saleLead.getRegistrationNumber(), lineItemList);

                statusMessage = "New Sale Inquiry Saved Successfully";
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Add New Sale Inquiry Result: " + statusMessage + " (New Sale Inquiry ID is " + saleInquiryId.toString() + ")", ""));
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
